package com.ttbj.controller.notify.paramHandler;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ttbj.core.util.XMLUtil;

public class ParamsHandler {

	/**
	 * 阿里回调
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, String> aliPayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		// 获取支付宝POST过来的反馈信息
		Map<String, String[]> requestParams = request.getParameterMap();
		Iterator<Map.Entry<String, String[]>> it = requestParams.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String[]> entry = it.next();
			String key = entry.getKey();
			String[] values = entry.getValue();
			String value = "";
			// 这里如果数组的长度是1，说明只有一个，直接赋值就好，如果超过一个，后面加一个逗号来隔离
			for (int i = 0; i < values.length; i++) {
				value = values.length - 1 == i ? value + values[i] : value + values[i] + ",";
			}
			params.put(key, value);
		}
		if (params.isEmpty()) {
			return null;
		}
		// 获取触发通知类型 交易关闭，交易完结，交易成功，交易创建。
		String status = params.get("trade_status");
		BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
		outputStream.write("success".getBytes());
		outputStream.flush();
		outputStream.close();
		// 如果是交易完结或交易成功
		if ("TRADE_SUCCESS".equals(status) || "TRADE_FINISHED".equals(status)) {
			return params;
		} else if ("WAIT_BUYER_PAY".equals(status)) { // 如果状态是正在等待用户付款
			return null;
		} else if ("TRADE_CLOSED".equals(status)) { // 如果状态是未付款交易超时关闭，或支付完成后全额退款
			return null;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public SortedMap<Object, Object> weixinPayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		// 读取参数
		InputStream inputStream = request.getInputStream();
		StringBuffer sBuffer = new StringBuffer();
		String str;
		BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
		while ((str = bReader.readLine()) != null) {
			sBuffer.append(str);
		}
		bReader.close();
		inputStream.close();

		// 解析xml成map
		Map<String, String> map = new HashMap<String, String>();
		map = XMLUtil.doXMLParse(sBuffer.toString());
		if (map.isEmpty()) {
			return null;
		}
		// 过滤空 设置TreeMapp
		SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
		Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			String key = entry.getKey();
			String val = entry.getValue();
			String value = "";
			if (null != val) {
				value = val.trim();
			}
			packageParams.put(key, value);
		}
		if (map.isEmpty()) {
			return null;
		}
		String resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
		BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
		outputStream.write(resXml.getBytes());
		outputStream.flush();
		outputStream.close();
		return packageParams;
	}

}
